﻿/*
 * Crée par SharpDevelop.
 * Utilisateur: Propriétaire
 * Date: 21/01/2012
 * Heure: 09:21
 * 
 * Pour changer ce modèle utiliser Outils | Options | Codage | Editer les en-têtes standards.
 */
using System;
using System.Windows.Forms;

namespace IPD_GeneralLib
{
    /// <summary>
    /// Description of Affiche_enreg.
    /// </summary>
public partial class Csv_Choose_and_resolve : Form
 {
    
public Csv_Choose_and_resolve()
    {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();
            
            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
        }

string scsvfname = "";

void Csv_Choose_and_resolveLoad(object sender, EventArgs e)
 {
  this.Text = "Import a csv file (SMS only) & create an IPD (SMS only)" ;
  button3.Visible = false;
  label5.Text = "";
  textBox2.Text = IPDGeneral.sHelpImportCsv; // Help to import csv

  listView2.Items.Clear();
  
  for (int ii = 0 ; ii < IPDGeneral.sSms_Col_Names.Length ; ii++)
       listView1.Items.Add(IPDGeneral.sSms_Col_Names[ii]);
       
#if TRACE
scsvfname = @"D:\Develop_Projects\SharpDevelop Projects\BbIpdManaging-V0.99\BbIpdManaging\bin\Debug\exported_papa_6 sms_idem ipd.csv";
//scsvfname = @"D:\Develop_Projects\SharpDevelop Projects\BbIpdManaging-V0.99\BbIpdManaging\bin\Debug\exported_esther_only_1 sms_MONOLINE.csv";
//scsvfname = @"D:\Develop_Projects\SharpDevelop Projects\BbIpdManaging-V0.99\BbIpdManaging\bin\Debug\exported_esther_only_3 sms_multiine.csv";
//scsvfname = @"D:\Develop_Projects\SharpDevelop Projects\BbIpdManaging-V0.99\BbIpdManaging\bin\Debug\exported_esther_only_9sms_1line_quoted.csv";
Select_a_csv_file();
#endif
}
        
// Close Form
void Button1Click(object sender, EventArgs e)
 {
   Close() ;
}
        
// Select a csv file        
void Button2Click(object sender, EventArgs e)
 {
  label5.Text = "";
  textBox2.Text = IPDGeneral.sHelpImportCsv; // Help to import csv
	openFileDialog1.Filter = "Csv File (*.csv)|*.csv|" +
	                         "Text File (*.txt)|*.txt|" +
		                     "All Files (*.*)|*.*" ;
	openFileDialog1.FileName = "" ;
	
	if (MainForm.ARGVDIR != "")
	   openFileDialog1.InitialDirectory = MainForm.ARGVDIR ;
	
	if (openFileDialog1.ShowDialog()!= DialogResult.OK)
        return ;
		
	scsvfname = openFileDialog1.FileName;
	
	openFileDialog1.Dispose();
  Select_a_csv_file();
}
        
// Select a csv file        
void Select_a_csv_file()
 {
    if (scsvfname == "")
       return;
	
	textBox1.Text = scsvfname ;
    button3.Visible = true;
    label5.Text = "";
    listView2.Items.Clear();
    
    int iret = IPDGeneral.ReadCsvFile(scsvfname);
    
    if (iret >= 0)
       {
        int ii = 0;
        for (ii = 0 ; ii < IPDGeneral.sCSV_colnames.Length ; ii++)
           {
            listView2.Items.Add(IPDGeneral.sCSV_colnames[ii]);
            listView2.Items[ii].Tag = ii;
         }
        for (; ii < IPDGeneral.sSms_Col_Names.Length ; ii++)
           {
            listView2.Items.Add("[default]");
            listView2.Items[ii].Tag = -1;
         }
      }
      else label5.Text = "Error: Csv file problem";
}

// Launch create IPD process
void Button3Click(object sender, EventArgs e)
 { 
    int ijj = 0;
    
    if (listView2.Items.Count == 0)
        return;  // No valid csv file selected
    
    label5.Text = "Please wait...";
    
    for (int ii = 0 ; ii < IPDGeneral.sSms_Col_Names.Length ; ii++)
        {
         ijj = (int) listView2.Items[ii].Tag;
         IPDGeneral.iSMS_fieldresolved[ii] = ijj;
      }

    int iret = IPDGeneral.Create_OneDB_Ipd();
    if (iret < 0)
       label5.Text = "Error: Ipd create OR Csv fields problems";
           else  label5.Text = "Done.\r\n" + "File: " + IPDGeneral.sIpd_filename + " created";
}

// UP listView2        
void Button4Click(object sender, EventArgs e)
 {
    int inbitem = listView2.Items.Count;
    if (inbitem == 0)
        return ;
    
    int iselindex = listView2.SelectedIndices[0];
    if (iselindex <= 0) // No selection OR already or first line
        return ;
    
    ListViewItem oprevitem = listView2.Items[iselindex];
    listView2.Items[iselindex].Remove();
    listView2.Items.Insert(iselindex -1, oprevitem);

    listView2.Focus();  
    listView2.Items[iselindex - 1].Selected = true;
}

// DOWN button
void Button5Click(object sender, EventArgs e)
 {
    int inbitem = listView2.Items.Count;
    if (inbitem == 0)
        return ;
    
    int iselindex = listView2.SelectedIndices[0];
    if ((iselindex == inbitem - 1) || (iselindex == -1)) // No selection OR already last
        return ;
    
    ListViewItem oprevitem = listView2.Items[iselindex];
    listView2.Items[iselindex].Remove();
    listView2.Items.Insert(iselindex + 1, oprevitem);

    listView2.Focus();  
    listView2.Items[iselindex + 1].Selected = true;
}

        
void Button6Click(object sender, EventArgs e)
 {
  textBox2.Text = IPDGeneral.sHelpImportCsv; // Help to import csv
}
        
void Button7Click(object sender, System.EventArgs e)
 {
    textBox2.Text = "";
    int ilmx = IPDGeneral.VirtualArray_SMS_Out.GetLength(1);
    if (ilmx > 10)
        ilmx = 10;
    for (int ii = 0 ; ii < ilmx ; ii++)
        textBox2.Text += IPDGeneral.VirtualArray_SMS_Out[0, ii] + "\r\n";
}

  }
}
